package com.example.sikong.handler.websocket;

import com.example.mqtt.utils.MqttClientUtils;
import com.example.config.SiKongProperties;
import com.example.sikong.handler.mqtt.SiKongOsdMqttMessageListener;
import com.example.sikong.handler.mqtt.SiKongStateMqttMessageListener;
import com.example.sikong.service.RedisProvider;
import com.example.websocket.handler.DefaultWebSocketHandler;
import com.example.websocket.handler.OrderdWebSocketHandler;
import com.example.websocket.handler.RegisterWebSocketHandler;
import com.example.websocket.util.SpringUtils;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketSession;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 *
 * ws 消息处理器
 * @see RegisterWebSocketHandler
 * */
@Component
public class SiKongWebSocketHandler extends DefaultWebSocketHandler implements InitializingBean {
    public static final String type = "fh.dji.com";
    @Autowired
    private RedisProvider redisProvider;
    @Autowired
    private SiKongProperties siKongProperties;



    public SiKongWebSocketHandler() {
        super(new String[]{"/ws/fh-dji-com/**"}, new ArrayList<>());
    }


    public String getUniqueCode(WebSocketSession session) {
        return type;
    }



    /**
     * 启动websockt 后根据配置
     * */
    @Override
    public void afterPropertiesSet() throws Exception {

        // 注册 mqtt 消息处理器
        Map<String, OrderdWebSocketHandler> beans = SpringUtils.getBeans(OrderdWebSocketHandler.class);
        List<OrderdWebSocketHandler> collect = beans.values().stream().filter(ele -> type.equals(ele.getType())).collect(Collectors.toList());
        handlers.addAll(collect);
        handlers.sort(Comparator.comparing(OrderdWebSocketHandler::getOrder));
        /*
         * 监听mqtt的时时数据和状态数据
         * */
        String broker = siKongProperties.getMqttServerIp(); // MQTT服务器地址
        String clientId = siKongProperties.getClientId();//
        String username = siKongProperties.getUsername();
        String password = siKongProperties.getPassword();
        boolean cleanSession = siKongProperties.isCleanSession();
        boolean automaticReconnect = siKongProperties.isAutomaticReconnect();
        MqttClientUtils instance = MqttClientUtils.getInstance(broker, clientId);
        MqttConnectOptions options = new MqttConnectOptions();
        options.setCleanSession(cleanSession);
        options.setUserName(username);
        options.setPassword(password.toCharArray());
        options.setAutomaticReconnect(automaticReconnect);
        instance.connect(options);
        instance.subscribe("thing/product/+/osd",new SiKongOsdMqttMessageListener(redisProvider));
        instance.subscribe("thing/product/+/state",new SiKongStateMqttMessageListener(redisProvider));
    }
}
